package ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import com.wyz.troika_test.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import presenter.BasePresenter;

/**
 * Created by 10153 on 2018/7/14.
 */

public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity {
    
    private boolean isToolBarInit = false;
    
    protected P mPresenter;
    @BindView(R.id.toolbar) protected Toolbar mToolBar = null;
    @BindView(R.id.coordinator) protected CoordinatorLayout mCoorLayout;
    @BindView(R.id.app_bar_layout) protected AppBarLayout mAppBarLayout;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ButterKnife.bind(this);
        init();
        
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        mPresenter.onStart();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        mPresenter.onPause();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.onResume();
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        mPresenter.onStop();
    }
    
    @Override
    protected void onDestroy() {
        mPresenter.onDestroy();
        mPresenter.onDetachView();
        this.mPresenter = null;
        super.onDestroy();
    }
    
    protected void init(){
        initPresenter();
        initToolBar();
    }
    
    private void initToolBar(){
        if(mToolBar == null){
            throw  new NullPointerException("please add a toolbar in your layout!");
        }
        setSupportActionBar(mToolBar);
        isToolBarInit = true;
    }
    
    public void setToolBarTitle(String title, boolean showHome){
        if(isToolBarInit){
            getSupportActionBar().setTitle(title);
            getSupportActionBar().setDisplayShowHomeEnabled(showHome);
            getSupportActionBar().setDisplayHomeAsUpEnabled(showHome);
        }else{
            throw new NullPointerException("please init a toolbar!");
        }
        
    }
    
    public P getPresenter(){
        return this.mPresenter;
    }
    
    
    public abstract int getLayout();
    
    public abstract void initPresenter();
}
